/**
 * 初始化prdTrace详情对话框
 */
var BusPrdTraceInfoDlg = {
    busPrdTraceInfoData : {},
    validateFields: {
        prdTraceMinimum: {
            validators: {
                notEmpty: {
                    message: '开始值不能为空'
                }
            }
        },
        prdTraceBatchcount: {
            validators: {
                notEmpty: {
                    message: '生成数量不能为空'
                }
            }
        },
        prdId: {
            validators: {
                notEmpty: {
                    message: '产品不能为空'
                }
            }
        }
    }
};
/**
 * 验证数据是否为空
 */
BusPrdTraceInfoDlg.validate = function () {
    $('#busPrdTraceInfoForm').data("bootstrapValidator").resetForm();
    $('#busPrdTraceInfoForm').bootstrapValidator('validate');
    return $("#busPrdTraceInfoForm").data('bootstrapValidator').isValid();
}
/**
 * 清除数据
 */
BusPrdTraceInfoDlg.clearData = function() {
    this.busPrdTraceInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
BusPrdTraceInfoDlg.set = function(key, val) {
    this.busPrdTraceInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
BusPrdTraceInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
BusPrdTraceInfoDlg.close = function() {
    parent.layer.close(window.parent.BusPrdTrace.layerIndex);
}

/**
 * 收集数据
 */
BusPrdTraceInfoDlg.collectData = function() {
    this
    .set('comId')
    .set('prdTraceMaximum')
    .set('prdTraceMinimum')
    .set('prdTraceBatchcount')
    .set('prdId')
    .set('status')
    .set('createtime')
    .set('version')
    .set('maintainer')
    .set('delFlag');
}

/**
 * 提交添加
 */
BusPrdTraceInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/busPrdTrace/add", function(data){
        console.log(data);
        if(data.code==200)
        {
            window.parent.BusPrdTrace.table.refresh();
            BusPrdTraceInfoDlg.close();
        }
        if(data.code==201)
        {
            Feng.error("存在重复的编号，请重新填写生成区间");
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.busPrdTraceInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
BusPrdTraceInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/busPrdTrace/update", function(data){
        Feng.success("修改成功!");
        window.parent.BusPrdTrace.table.refresh();
        BusPrdTraceInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.busPrdTraceInfoData);
    ajax.start();
}
function getProdutList4ComId(comId) {
    //提交信息
    $("#prdId").html('');
    var ajax = new $ax(Feng.ctxPath + "/busPrductInfo/list4ComId", function(data){
        var optionHtml='<option value="">请选择产品</option>';
        for(var i=0;i<data.length;i++){
            optionHtml+='<option value="'+data[i].id+'">'+data[i].prdName+'</option>';
        }
        $("#prdId").html(optionHtml);
    },function(data){
        Feng.error("获取产品失败!" + data.responseJSON.message + "!");
    });
    var data={"comId":comId};
    ajax.set(data);
    ajax.start();
}
function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentDeptMenu" || $(
            event.target).parents("#parentDeptMenu").length > 0)) {
        BusPrdTraceInfoDlg.hideDeptSelectTree();
    }
}
/**
 * 显示部门选择的树
 *
 * @returns
 */
BusPrdTraceInfoDlg.showDeptSelectTree = function() {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentDeptMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
BusPrdTraceInfoDlg.hideDeptSelectTree = function() {
    $("#parentDeptMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}
/**
 * 点击部门ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
BusPrdTraceInfoDlg.onClickDept = function(e, treeId, treeNode) {
    $("#pName").attr("value", BusPrdTraceInfoDlg.zTreeInstance.getSelectedVal());
    $("#comId").attr("value", treeNode.id);
    getProdutList4ComId(treeNode.id);
}
$(function() {
    Feng.initValidator("busPrdTraceInfoForm", BusPrdTraceInfoDlg.validateFields);
    var ztree = new $ZTree("parentDeptMenuTree", "/dept/tree");
    ztree.bindOnClick(BusPrdTraceInfoDlg.onClickDept);
    ztree.init();
    BusPrdTraceInfoDlg.zTreeInstance = ztree;
    init_city_select($("#citySelect"));


});
